package com.company.cloud.mall.modules.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.mall.constant.ShopConstant;
import com.company.cloud.mall.modules.app.models.PayApplyModel;
import com.company.cloud.mall.modules.app.models.PayResultModel;
import com.company.cloud.mall.modules.app.service.ShopOnlinePayService;
import com.company.cloud.pay.client.PayRemoteServiceImpl;
import com.company.cloud.pay.modules.api.request.PayOrderQueryRequest;
import com.company.cloud.pay.modules.api.request.PayOrderRequest;
import com.company.cloud.pay.modules.api.response.PayOrderQueryResponse;
import com.company.cloud.pay.modules.api.response.PayOrderResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 订单第三方支付服务
 */
@Service
public class ShopOnlinePayServiceImpl implements ShopOnlinePayService {

//	@Autowired
//	private PayRemoteService payRemoteService;

    @Autowired
    private PayRemoteServiceImpl payRemoteService;

    /**
     * 第三方支付申请
     */
    @Override
    public PayApplyModel payApply(String companyNo, String sn, BigDecimal fund, String payTradeType, String returnUrl, LoginSimpleUserInfo userInfo) {
        PayOrderRequest payReq = new PayOrderRequest();
        payReq.setBody("商品");
        payReq.setOutTradeNo(sn);
        payReq.setTotalFee(fund.multiply(new BigDecimal(100)).intValue());
        payReq.setOpenid(userInfo.getThirdOpenId());
        payReq.setServiceCode(ShopConstant.Pay_Service_No);
        payReq.setCompanyNo(companyNo);
        payReq.setPayTradeType(payTradeType);
        payReq.setReturnUrl(returnUrl);
        payReq.setOpUserId(userInfo.getId());
        payReq.setOpIp(userInfo.getIp());

        BaseClientResult<PayOrderResponse> payResp = payRemoteService.payOrderInfo(payReq);
        PayOrderResponse payRespData = BaseClientResult.getResultData2Error(payResp);
        if (payResp == null || !"SUCCESS".equals(payRespData.getRetCode())) {
            throw new BusinessException("99", "支付错误:" + payRespData.getRetMsg());
        }
        return new PayApplyModel(sn, fund, payRespData.getBiz());
    }


    /**
     * 第三方支付退款申请
     */
    @Override
    public String payRefundApply(String companyNo, String payChannel, String paySn, BigDecimal payFund, String refundSn,
                                 BigDecimal refundFund, String remark) {

        //todo:
//		try {
//			WXPay pay= WXPay.getInstall(wxPayConfig);
//			Map<String, String> payData=new HashMap<String, String>();
//			payData.put("out_trade_no", paySn) ; //商户订单号
//			payData.put("out_refund_no", refundSn) ; //商户退款单号
//			payData.put("total_fee", String.valueOf(payFund.multiply(new BigDecimal(100)).intValue())) ; //订单金额(分)
//			payData.put("refund_fee", String.valueOf(refundFund.multiply(new BigDecimal(100)).intValue())) ; //退款金额(分)
//			if(StringUtils.hasText(remark)) {
//				payData.put("refund_desc", remark) ; //退款原因
//			}
//			if(StringUtils.hasText(wxPayConfig.getRefundNotifyUrl())) {
//				payData.put("notify_url", wxPayConfig.getRefundNotifyUrl()) ; //退款结果通知url
//			}
//			Map<String, String> payReturnData=pay.refund(payData);
//			return payReturnData.get("result_code");
//		} catch (Exception e) {
//			e.printStackTrace();
//			throw new BusinessException(MallErrorEnum.Order_WX_Pay);
//		}
        return null;
    }


    /**
     * 第三方支付结果查询
     */
    @Override
    public PayResultModel queryPayResult(String companyNo, String paysn, String paymentChannel) {
        PayOrderQueryRequest payReq = new PayOrderQueryRequest();
        payReq.setOutTradeNo(paysn);
        payReq.setServiceCode(ShopConstant.Pay_Service_No);
        payReq.setCompanyNo("PTC");
        payReq.setPayTradeType(paymentChannel);
        payReq.setOpUserId(0);
        payReq.setOpIp("127.0.0.1");
        BaseClientResult<PayOrderQueryResponse> payRespResult = payRemoteService.payOrderQuery(payReq);
        if (payRespResult != null) {
            System.out.println(JSON.toJSONString(payRespResult));
        } else {
            System.out.println("++++++++++++++++ 返回值为空");
        }
//		PayOrderQueryResponse payResp= payRemoteService.payOrderQuery(payReq);
        PayOrderQueryResponse payResp = BaseClientResult.getResultData2Error(payRespResult);
        if (!"SUCCESS".equals(payResp.getRetCode())) {
            throw new BusinessException("99", "支付错误:" + payResp.getRetMsg());
        }
        return new PayResultModel(payResp.getState(), payResp.getThirdPayState());
    }


    /**
     * 第三方支付退款结果查询
     */
    @Override
    public PayResultModel queryPayRefundResult(String companyNo, String paysn, String paymentChannel) {
        return null;
    }

}
